﻿using Microsoft.WindowsAzure.Storage.Queue;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using wlog.Model;

namespace wlog.Shell.Storage.Queue
{
    public class QueuePushCommand : QueueStorageCommand
    {
        public override void Setup(string[] args)
        {
            base.Setup(args);
            base.CreateTask(() => { RunCommand(); });
        }
        public OutputChannel RunCommand()
        {
            try
            {
                string otype = this.objectName;
                var QueueAPI = GetQueueAccount();
                var queue = QueueAPI.GetQueueReference(otype);
                bool created = queue.CreateIfNotExists();
                Results.WriteLine("Queue Created {0} ", created);
                try
                {

                    string location = additionalCommands.ContainsKey(K_OPTIONS.objectlocation) ? additionalCommands[K_OPTIONS.objectlocation] : K_LOCATION.inline.ToString();
                    string filePath = additionalCommands.ContainsKey(K_OPTIONS.objecturi) ? additionalCommands[K_OPTIONS.objecturi] : string.Empty;
                    string inlinemessage = additionalCommands.ContainsKey(K_OPTIONS.objectmessage) ? additionalCommands[K_OPTIONS.objectmessage] : string.Empty;

                    byte[] buffer;
                    if (location == K_LOCATION.file.ToString())
                    {
                        Results.WriteLine("Reading files from Disk {0} ", filePath);
                        buffer = File.ReadAllBytes(filePath);
                    }
                    else if (location == K_LOCATION.http.ToString() || location == K_LOCATION.https.ToString()) //http
                    {
                        Results.WriteLine("Reading files from Website {0} ", filePath);
                        WebClient client = new WebClient();
                        buffer = client.DownloadData(filePath);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(inlinemessage))
                        {
                            buffer = UTF8Encoding.UTF8.GetBytes(inlinemessage);
                        }
                        else
                        {
                            buffer = UTF8Encoding.UTF8.GetBytes(filePath);
                        }
                    }
                    CloudQueueMessage message = new CloudQueueMessage(buffer);
                    Results.WriteLine("Pushing message to {0} ", objectName);
                    queue.AddMessage(message);
                    Results.WriteLine("Message Pushed to to {0} ", objectName);
                }
                catch (Exception fvds)
                {

                }
            }
            catch (Exception azureEx)
            {
                Results.WriteLine("Error enqueuing Queue {0} ", azureEx.Message);
            }
            return Results;
        }

    }
}
